package com.stylefeng.guns.core.util;

import java.net.URL;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.client.methods.HttpGet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class WeekUtils {
	
	private static Logger log = LoggerFactory.getLogger(WeekUtils.class);
	
	private static final HttpClientUtil clientUtil = new HttpClientUtil();
	private static String API_PATH = "http://tool.bitefu.net/jiari/?d=%s";
	private static String K780_API_PATH = "http://api.k780.com/?app=life.workday&date=%s&appkey=34063&sign=31d8ac39a62d9238d08356af18fcedef&format=json";

	public static boolean isHoliday(String date){
		HttpGet httpGet = HttpClientUtil.getGetRequest();
		try {
			httpGet.setURI(new URL(String.format(API_PATH, date)).toURI());
			String json = clientUtil.executeAndGetWithFailedRetry(httpGet, 6,10001);
			if(json.endsWith("0")){
				return true;
			}
			return false;
		} catch (Exception e) {
			log.error("get gupiao data is error!" + e);
			return false;
		}finally{
			httpGet.abort();
		}
	}
	
	public static boolean isHolidayByK780(String date){
		HttpGet httpGet = HttpClientUtil.getGetRequest();
		try {
			httpGet.setURI(new URL(String.format(K780_API_PATH, date)).toURI());
			String json = clientUtil.executeAndGetWithFailedRetry(httpGet, 6,10001);
			JSONObject jsonObj = JSON.parseObject(JSON.parseObject(json).getString("result"));
			if(jsonObj.containsKey("worknm")){
				if(jsonObj.getString("worknm").equals("假日")){
					return false;
				}else if(jsonObj.getString("worknm").equals("工作日") && jsonObj.getString("week_3").equals("周六") ||
						jsonObj.getString("week_3").equals("周日")){
					return false;
				}else{
					return true;
				}
			}
			return false;
		} catch (Exception e) {
			log.error("get gupiao data is error!" + e);
			return false;
		}finally{
			httpGet.abort();
		}
	}
	
	public static void main(String[] args) {
		System.out.println(isHolidayByK780("20180928"));
	}
}
